﻿using System;



namespace MathUtility

{

    // 数学工具类

    class MyMath

    {

        // 加法

        public static double Add(double a, double b)

        {

            return a + b;

        }



        // 减法

        public static double Subtract(double a, double b)

        {

            return a - b;

        }



        // 乘法

        public static double Multiply(double a, double b)

        {

            return a * b;

        }



        // 除法

        public static double Divide(double a, double b)

        {

            if (b == 0)

            {

                throw new DivideByZeroException("除数不能为零");

            }

            return a / b;

        }



        // 重载方法：整数运算

        public static int Add(int a, int b)

        {

            return a + b;

        }



        public static int Subtract(int a, int b)

        {

            return a - b;

        }



        public static int Multiply(int a, int b)

        {

            return a * b;

        }



        public static double Divide(int a, int b)

        {

            if (b == 0)

            {

                throw new DivideByZeroException("除数不能为零");

            }

            return (double)a / b;

        }



        // 显示运算结果

        public static void DisplayResult(string operation, double a, double b, double result)

        {

            Console.WriteLine($"{a} {operation} {b} = {result}");

        }



        public static void DisplayResult(string operation, int a, int b, int result)

        {

            Console.WriteLine($"{a} {operation} {b} = {result}");

        }



        public static void DisplayResult(string operation, int a, int b, double result)

        {

            Console.WriteLine($"{a} {operation} {b} = {result:F4}");

        }

    }



    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("数学工具类测试");

            Console.WriteLine("==============");

            Console.WriteLine();



            // 测试双精度浮点数运算

            Console.WriteLine("双精度浮点数运算:");

            double a = 10.5;

            double b = 3.2;



            double resultAdd = MyMath.Add(a, b);

            MyMath.DisplayResult("+", a, b, resultAdd);



            double resultSubtract = MyMath.Subtract(a, b);

            MyMath.DisplayResult("-", a, b, resultSubtract);



            double resultMultiply = MyMath.Multiply(a, b);

            MyMath.DisplayResult("*", a, b, resultMultiply);



            try

            {

                double resultDivide = MyMath.Divide(a, b);

                MyMath.DisplayResult("/", a, b, resultDivide);

            }

            catch (DivideByZeroException ex)

            {

                Console.WriteLine($"错误: {ex.Message}");

            }



            Console.WriteLine();



            // 测试整数运算

            Console.WriteLine("整数运算:");

            int x = 15;

            int y = 4;



            int intResultAdd = MyMath.Add(x, y);

            MyMath.DisplayResult("+", x, y, intResultAdd);



            int intResultSubtract = MyMath.Subtract(x, y);

            MyMath.DisplayResult("-", x, y, intResultSubtract);



            int intResultMultiply = MyMath.Multiply(x, y);

            MyMath.DisplayResult("*", x, y, intResultMultiply);



            try

            {

                double intResultDivide = MyMath.Divide(x, y);

                MyMath.DisplayResult("/", x, y, intResultDivide);

            }

            catch (DivideByZeroException ex)

            {

                Console.WriteLine($"错误: {ex.Message}");

            }



            Console.WriteLine();



            // 测试边界情况

            Console.WriteLine("边界情况测试:");



            // 除数为零

            try

            {

                double zeroDivision = MyMath.Divide(10, 0);

                MyMath.DisplayResult("/", 10, 0, zeroDivision);

            }

            catch (DivideByZeroException ex)

            {

                Console.WriteLine($"除数为零测试: {ex.Message}");

            }



            // 负数运算

            double negativeResult = MyMath.Add(-5, 3);

            MyMath.DisplayResult("+", -5, 3, negativeResult);



            // 大数运算

            double largeResult = MyMath.Multiply(1e10, 2e10);

            Console.WriteLine($"1e10 * 2e10 = {largeResult:E}");



            Console.WriteLine();



            // 用户交互测试

            Console.WriteLine("用户交互测试:");

            Console.Write("请输入第一个数字: ");

            double num1 = Convert.ToDouble(Console.ReadLine());



            Console.Write("请输入第二个数字: ");

            double num2 = Convert.ToDouble(Console.ReadLine());



            Console.Write("请选择运算 (+, -, *, /): ");

            string operation = Console.ReadLine();



            double userResult = 0;

            bool validOperation = true;



            switch (operation)

            {

                case "+":

                    userResult = MyMath.Add(num1, num2);

                    break;

                case "-":

                    userResult = MyMath.Subtract(num1, num2);

                    break;

                case "*":

                    userResult = MyMath.Multiply(num1, num2);

                    break;

                case "/":

                    try

                    {

                        userResult = MyMath.Divide(num1, num2);

                    }

                    catch (DivideByZeroException ex)

                    {

                        Console.WriteLine($"错误: {ex.Message}");

                        validOperation = false;

                    }

                    break;

                default:

                    Console.WriteLine("无效的运算符号");

                    validOperation = false;

                    break;

            }



            if (validOperation)

            {

                MyMath.DisplayResult(operation, num1, num2, userResult);

            }



            Console.ReadLine();

        }

    }

}